package com.dp.appi.controller;

import com.dp.appi.dao.CodesetDao;
import com.dp.appi.entity.MaterialEntity;
import com.dp.appi.entity.UserEntity;
import com.dp.appi.service.CodesetService;
import com.dp.appi.service.ConsumerService;
import com.dp.appi.service.MaterialService;
import com.dp.appi.service.ProjectService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2016/4/27.
 */
@Controller
@RequestMapping(value = "/codesetControl")
public class CodesetControl {
    private static Logger log = LoggerFactory.getLogger(CodesetControl.class);
    @Autowired
    private CodesetService codesetService;
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private CodesetDao codesetDao;

    /**
     * 获取点位信息
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/ptList", method = RequestMethod.GET)
    public void ptList(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            modelMap.addAttribute("codesetList", codesetService.ptList());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取导线类型列表
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/wireList", method = RequestMethod.GET)
    public void wireList(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            modelMap.addAttribute("wireList", codesetService.wireList(param));
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取典设版本
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/codesetVersion", method = RequestMethod.GET)
    public void codesetVersion(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            int codesetType = 0;
            Object type = param.get("codeset_type");
            if (type == null || type.equals(""))
                codesetType = 0;
            else
                codesetType = Integer.parseInt(String.valueOf(type));
            if (codesetType != 15)
                modelMap.addAttribute("codesetVersion", codesetService.codesetVersion(codesetType));
            else
                modelMap.addAttribute("codesetVersion", codesetService.codesetVersionShouChang());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 获取典设版本
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/addcodeset", method = RequestMethod.POST)
    public void addcodeset(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            //添加一个项目版本
            String pro_name = param.get("pro_name");
            String www = param.get("pro_id");
            if (!www.equals("")) {

                Long staes_id = Long.valueOf(param.get("pro_id"));
                Map<String, Object> pro = projectService.projectInfo(staes_id);
                if (pro != null && pro.get("project_id") != null) {

                    Long pro_id = Long.valueOf(pro.get("project_id").toString());
                    List<Map<String, Object>> versionList = codesetService.GetcodesetVersionListByProid(pro_id);
                    Long codesetVersionid = Long.valueOf(0);
                    //判断该工程有无版本
                    if (versionList.size() > 0) {
                        codesetVersionid = Long.valueOf(versionList.get(0).get("codeset_version_id").toString());

                    } else {
                        codesetVersionid = codesetService.addcodesetVersion(pro_id, pro_name);
                    }
                    if (codesetVersionid > 0) {
                        String codesetname = param.get("codesetname");
                        String codesetnumber = param.get("codesetnumber");
                        Long ptid = Long.valueOf(param.get("ptid"));
                        String keyword = param.get("keyword");
                        if (!codesetname.equals("") && ptid > 0) {
                            Long sss=   codesetService.addcodesetNew(codesetVersionid, codesetname, codesetnumber, ptid, keyword);
                            modelMap.addAttribute("codesetid",sss);
                            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
                        } else {
                            modelMap.addAllAttributes(SYS.EXPO016.msgMap());
                        }
                    } else {
                        modelMap.addAllAttributes(SYS.EXPO016.msgMap());
                    }
                } else {
                    modelMap.addAllAttributes(SYS.EXPO016.msgMap());
                }
            } else {
                modelMap.addAllAttributes(SYS.EXPO003.msgMap());
            }


        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 修改
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/uupcodesetVersion", method = RequestMethod.POST)
    public void uupcodesetVersion(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            //添加一个项目版本
            String pro_name = param.get("pro_name");
            String www = param.get("pro_id");
            if (!www.equals("")) {
                Long staes_id = Long.valueOf(param.get("pro_id"));
                Map<String, Object> pro = projectService.projectInfo(staes_id);
                if (pro != null && pro.get("project_id") != null) {

                    Long pro_id = Long.valueOf(pro.get("project_id").toString());
                    List<Map<String, Object>> versionList = codesetService.GetcodesetVersionListByProid(pro_id);
                    Long codesetVersionid = Long.valueOf(0);
                    //判断该工程有无版本
                    if (versionList.size() > 0) {
                        codesetVersionid = Long.valueOf(versionList.get(0).get("codeset_version_id").toString());

                    } else {
                        codesetVersionid = codesetService.addcodesetVersion(pro_id, pro_name);
                    }
                    projectService.upProjectVersionid(codesetVersionid, pro_id);
                    String material_all = param.get("material_all");
                    List<MaterialEntity> param_all = new Gson().fromJson(material_all, new TypeToken<List<MaterialEntity>>() {
                    }.getType());
                    if (param_all.size() > 0 && codesetVersionid > 0) {
                        for (MaterialEntity paramone : param_all) {
                            if (!paramone.getName().equals("")) {
                                codesetDao.updatcodesetVersionMaterial(paramone.getType(),codesetVersionid,paramone.getSort());//修改材料表
                                codesetDao.updateCodesetMaterial(paramone.getSort(),paramone.getMaterial_number());//修改典设表
                                codesetDao.updateModelMaterial(paramone.getSort(),paramone.getMaterial_number());//模板
                                codesetDao.updatePexplorationMaterial(paramone.getSort(),paramone.getMaterial_number());//点位
                            }
                        }
                    }
                    modelMap.addAttribute("codesetVersionid", codesetVersionid);
                } else {
                    modelMap.addAllAttributes(SYS.E10003.msgMap());
                }
            } else {
                modelMap.addAllAttributes(SYS.E10003.msgMap());
            }

        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 添加典设版本和材料
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/aadcodesetVersion", method = RequestMethod.POST)
    public void aadcodesetVersion(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            //添加一个项目版本
            String pro_name = param.get("pro_name");
            String www = param.get("pro_id");
            if (!www.equals("")) {
                Long staes_id = Long.valueOf(param.get("pro_id"));
                Map<String, Object> pro = projectService.projectInfo(staes_id);
                if (pro != null && pro.get("project_id") != null) {

                    Long pro_id = Long.valueOf(pro.get("project_id").toString());
                    List<Map<String, Object>> versionList = codesetService.GetcodesetVersionListByProid(pro_id);
                    Long codesetVersionid = Long.valueOf(0);
                    //判断该工程有无版本
                    if (versionList.size() > 0) {
                        codesetVersionid = Long.valueOf(versionList.get(0).get("codeset_version_id").toString());

                    } else {
                        codesetVersionid = codesetService.addcodesetVersion(pro_id, pro_name);
                    }
//                    if (param.get("addNew") == null)
                        projectService.upProjectVersionid(codesetVersionid, pro_id);
                    //删除版本下的所有材料


                    String material_all = param.get("material_all");
                    List<MaterialEntity> param_all = new Gson().fromJson(material_all, new TypeToken<List<MaterialEntity>>() {
                    }.getType());
                    if (param_all.size() > 0 && codesetVersionid > 0) {
                        if (param.get("addNew") == null)//如果是导入导出则删除，手动新增不删除
                            codesetService.delVersionMaterialByid(codesetVersionid);
                        for (MaterialEntity paramone : param_all) {
                            if (!paramone.getName().equals("")) {
                                codesetService.addcodesetVersionMaterial(codesetVersionid,
                                        paramone.getMaterial_number(),
                                        paramone.getName(),
                                        paramone.getSpec(),
                                        paramone.getNum(),
                                        paramone.getUnit(), paramone.getType(), paramone.getSort());
                            }
                        }
                    }


                    modelMap.addAttribute("codesetVersionid", codesetVersionid);
                } else {
                    modelMap.addAllAttributes(SYS.E10003.msgMap());
                }
            } else {
                modelMap.addAllAttributes(SYS.E10003.msgMap());
            }

        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 添加典设版本和下面的有个模块，和默认的第一个材料
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/aadcodesetVersionAndmodleMaterial", method = RequestMethod.POST)
    public void aadcodesetVersionAndmodleMaterial(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {


            //添加一个项目版本
            String pro_name = param.get("pro_name");
            String www = param.get("pro_id");
            if (!www.equals("")) {
                Long staes_id = Long.valueOf(param.get("pro_id"));
                Map<String, Object> pro = projectService.projectInfo(staes_id);
                if (pro != null && pro.get("project_id") != null) {

                    Long pro_id = Long.valueOf(pro.get("project_id").toString());
                    List<Map<String, Object>> versionList = codesetService.GetcodesetVersionListByProid(pro_id);
                    Long codesetVersionid = Long.valueOf(0);
                    //判断该工程有无版本
                    if (versionList.size() > 0) {
                        codesetVersionid = Long.valueOf(versionList.get(0).get("codeset_version_id").toString());

                    } else {
                        codesetVersionid = codesetService.addcodesetVersion(pro_id, pro_name);
                    }
                    projectService.upProjectVersionid(codesetVersionid, pro_id);
                    //添加版本下面的模块和默认第一个材料
                    String modleName = param.get("modleName");
                    String apec = param.get("apec");
                    codesetService.addcodesetVersionModle(codesetVersionid, modleName, apec);

                    modelMap.addAttribute("codesetVersionid", codesetVersionid);
                } else {
                    modelMap.addAllAttributes(SYS.E10003.msgMap());
                }
            } else {
                modelMap.addAllAttributes(SYS.E10003.msgMap());
            }
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 复制一个典设到某个版本下
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/copyCodeset", method = RequestMethod.POST)
    public void copyCodeset(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {


            //添加一个项目版本
            String pro_name = param.get("pro_name");
            String www = param.get("pro_id");
            if (!www.equals("")) {
                Long staes_id = Long.valueOf(param.get("pro_id"));
                Long codesetid = Long.valueOf(param.get("codesetid"));
                Map<String, Object> pro = projectService.projectInfo(staes_id);
                if (pro != null && pro.get("project_id") != null) {

                    Long pro_id = Long.valueOf(pro.get("project_id").toString());
                    List<Map<String, Object>> versionList = codesetService.GetcodesetVersionListByProid(pro_id);
                    Long codesetVersionid = Long.valueOf(0);
                    //判断该工程有无版本
                    if (versionList.size() > 0) {
                        codesetVersionid = Long.valueOf(versionList.get(0).get("codeset_version_id").toString());

                    } else {
                        codesetVersionid = codesetService.addcodesetVersion(pro_id, pro_name);
                    }
                    projectService.upProjectVersionid(codesetVersionid, pro_id);

                    //复制典设主表
                    Long newcodesetid = codesetService.copyCodeset(codesetid, codesetVersionid);
                    // 复制典设材料表
                    codesetService.copyCodesetMaterial(codesetid, newcodesetid);
                    modelMap.addAttribute("codesetVersionid", codesetVersionid);


                }
            } else {
                modelMap.addAllAttributes(SYS.E10003.msgMap());
            }


        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 收藏典设
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/collectionCodeset", method = RequestMethod.POST)
    public void collectionCodeset(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {


            Long codesetid = Long.valueOf(param.get("codesetid"));
            String openIdent = param.get("openIdent");
            UserEntity userEntity = UserEntity.decrypt(openIdent);
            Map<String, Object> map = consumerService.getUser(userEntity);
            String userid = map.get("id").toString();
            //复制典设主表
            Long newcodesetid = codesetService.collectionCodeset(codesetid, userid);
            // 复制典设材料表
            codesetService.copyCodesetMaterial(codesetid, newcodesetid);
            modelMap.addAttribute(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 删除项目典设
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/delCodeset", method = RequestMethod.POST)
    public void delCodeset(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {


            Long codesetid = Long.valueOf(param.get("codesetid"));

            codesetService.delCodesetByid(codesetid);

            modelMap.addAttribute(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 修改项目典设
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/modfyCodesetNameById", method = RequestMethod.POST)
    public void modfyCodesetNameById(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {


            Long codesetid = Long.valueOf(param.get("codesetid"));
            String codesetname = param.get("codesetname").toString();
            String codesetnumber = param.get("codesetnumber").toString();
            codesetService.modfyCodesetNameById(codesetid, codesetname, codesetnumber);

            modelMap.addAttribute(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取典设列表
     *
     * @param modelMap
     * @param param
     * @param s        开始序列
     * @param n        获取条数
     */
    @RequestMapping(value = "/codesetList", method = RequestMethod.GET)
    public void codesetList(ModelMap modelMap, @RequestParam Map<String, String> param, Integer s, Integer n) {
        try {
            modelMap.addAttribute("codesetList", codesetService.codesetList(param.get("filter"), param.get("name"),
                    param.get("codeset_version_id"), param.get("pt_id"), param.get("keyword"), s, n));
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 获取模块列表
     *
     * @param modelMap
     * @param param
     * @param s        开始序列
     * @param n        获取条数
     */
    @RequestMapping(value = "/modleList", method = RequestMethod.GET)
    public void modleList(ModelMap modelMap, @RequestParam Map<String, String> param, Integer s, Integer n) {
        try {
            modelMap.addAttribute("modleList",
                    codesetService.modleList(param.get("name"),
                            param.get("codeset_version_id"), s, n));
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 24、 获取典设材料
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/codesetMaterial", method = RequestMethod.GET)
    public void codesetMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long codeset_id) {
        try {
            modelMap.addAttribute("materialList", codesetService.codesetMaterial(codeset_id));
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    @RequestMapping(value = "/codesetallcount", method = RequestMethod.POST)
    public void codesetallcount(ModelMap modelMap, @RequestParam Map<String, String> param, Long versionid) {
        try {

            String allcount = codesetService.versionidallcount(versionid);
            modelMap.addAttribute("allcount", allcount);
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 24、 获取模块材料
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/modleMaterial", method = RequestMethod.GET)
    public void modleMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long modle_id) {
        try {
            modelMap.addAttribute("materialList", codesetService.modelMaterial(modle_id));
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 24、 添加模块材料
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/addmodleMaterial", method = RequestMethod.POST)
    public void addmodleMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long modle_id, String material_number) {
        try {
            String[] arr = material_number.split("\\|");
            for (int i = 0; i < arr.length; i++) {
                codesetService.impModleMaterial(modle_id, arr[i]);
            }

            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 24、 删除模块材料
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/delmodleMaterial", method = RequestMethod.POST)
    public void delmodleMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long id) {
        try {
            codesetService.delmodleMaterial(id);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 24、 修改模块材料
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/modifymodleMaterial", method = RequestMethod.POST)
    public void modifymodleMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long id, double num) {
        try {
            codesetService.modifymodleMaterial(id, num);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }


    /**
     * 删除典设材料根据材料 id
     *
     * @param modelMap
     * @param param
     * @param id       模块id或者材料id
     */
    @RequestMapping(value = "/delcodesetMaterial", method = RequestMethod.POST)
    public void delcodesetMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long id) {
        try {
            codesetService.delcodesetMaterial(id);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            String str = ExceptionUtil.getExceptionAllinformation(e);
            if (str.indexOf("SQLErrorCodeSQLExceptionTranslator") != -1) {
                modelMap.addAllAttributes(SYS.EXPO006.msgMap());
            } else {
                modelMap.addAllAttributes(SYS.E10002.msgMap());
            }
            log.error(str);
            e.printStackTrace();
        }
    }

    /**
     * 典设导入模块  选中的模块id 和 典设
     *
     * @param modelMap
     * @param param
     * @param codeset_id
     * @param module_id
     */
    @RequestMapping(value = "/impcodesetModule", method = RequestMethod.POST)
    public void impcodesetModule(ModelMap modelMap, @RequestParam Map<String, String> param, Long codeset_id, String module_id) {
        try {
            String[] arr = module_id.split("\\|");
            for (int i = 0; i < arr.length; i++) {
                codesetService.impcodesetModule(Long.parseLong(arr[i]), codeset_id);
            }
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            e.printStackTrace();
        }
    }

    /**
     * 典设导入材料  选中的材料id 和 典设
     *
     * @param modelMap
     * @param param
     * @param codeset_id
     */
    @RequestMapping(value = "/impcodesetMaterial", method = RequestMethod.POST)
    public void impcodesetMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long codeset_id, String material_id) {
        try {
            String[] arr = material_id.split("\\|");
            for (int i = 0; i < arr.length; i++) {
                codesetService.impcodesetMaterial(Long.parseLong(arr[i]), codeset_id);
            }
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            e.printStackTrace();
        }
    }


    /**
     * 替换典设材料或模块，同时改数量
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/changeCodesetMaterialorModle", method = RequestMethod.POST)
    public void changeCodesetMaterialorModle(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            codesetService.changeCodesetMaterialorModle(param);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            e.printStackTrace();
        }
    }


    /**
     * 24、 根据点位材料id 换同名字的材料或模块
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/likeNameMaterial", method = RequestMethod.GET)
    public void likeNameMaterial(ModelMap modelMap, @RequestParam Map<String, String> param) {
//        matId   踏勘点位材料id
//        s   开始条数
//       n  显示条数

        String versionid = "0";
        if (param.containsKey("versionid")) {
            versionid = param.get("versionid");
        }
        Long matId = Long.valueOf(param.get("matId"));
        Integer s = Integer.valueOf(param.get("s"));
        Integer n = Integer.valueOf(param.get("n"));
        try {
            modelMap.addAttribute("list", codesetService.likeNameMList(versionid, matId, s, n));
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

    /**
     * 24、 根据典设材料id 换同名字的材料或模块
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/likeNameCodesetMaterial", method = RequestMethod.GET)
    public void likeNameCodesetMaterial(ModelMap modelMap, @RequestParam Map<String, String> param) {

//        s   开始条数
//       n  显示条数

        String versionid = "0";
        if (param.containsKey("versionid")) {
            versionid = param.get("versionid");
        }
        Long matId = Long.valueOf(param.get("matId"));
        Integer s = Integer.valueOf(param.get("s"));
        Integer n = Integer.valueOf(param.get("n"));
        try {
            modelMap.addAttribute("list", codesetService.likeNameCodesetMaterial(versionid, matId, s, n));
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }
    /**
     * 复制典设从点位材料复制生成典设
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/addcodesetbyExperence", method = RequestMethod.POST)
    public void addcodesetbyExperence(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            //添加一个项目版本
            String pro_name = param.get("pro_name");
            String www = param.get("pro_id");
            if (!www.equals("")) {

                Long staes_id = Long.valueOf(param.get("pro_id"));
                Map<String, Object> pro = projectService.projectInfo(staes_id);
                if (pro != null && pro.get("project_id") != null) {

                    Long pro_id = Long.valueOf(pro.get("project_id").toString());
                    List<Map<String, Object>> versionList = codesetService.GetcodesetVersionListByProid(pro_id);
                    Long codesetVersionid = Long.valueOf(0);
                    //判断该工程有无版本
                    if (versionList.size() > 0) {
                        codesetVersionid = Long.valueOf(versionList.get(0).get("codeset_version_id").toString());

                    } else {
                        codesetVersionid = codesetService.addcodesetVersion(pro_id, pro_name);
                    }
                    if (codesetVersionid > 0) {
                        String codesetname = param.get("codesetname");
                        String codesetnumber = param.get("codesetnumber");
                        Long ptid = Long.valueOf(param.get("ptid"));
                        String keyword = param.get("keyword");
                        if (!codesetname.equals("") && ptid > 0) {
                            Long codesetid=   codesetService.addcodesetNew(codesetVersionid, codesetname, codesetnumber, ptid, keyword);


                            Long exploration_id = Long.valueOf(param.get("exploration_id"));
                            codesetService.copyCodesetExperence(exploration_id,codesetid);
                            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
                        } else {
                            modelMap.addAllAttributes(SYS.EXPO016.msgMap());
                        }
                    } else {
                        modelMap.addAllAttributes(SYS.EXPO016.msgMap());
                    }
                } else {
                    modelMap.addAllAttributes(SYS.EXPO016.msgMap());
                }
            } else {
                modelMap.addAllAttributes(SYS.EXPO003.msgMap());
            }


        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            log.error(ExceptionUtil.getExceptionAllinformation(e));
        }
    }

}
